package com.example.hospitalmanagement.activity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.hospitalmanagement.R
import com.example.hospitalmanagement.adapter.ChargeAdapter
import com.example.hospitalmanagement.bean.ChargeData
import com.example.hospitalmanagement.viewmodel.ChargeViewModel
import com.xiasuhuei321.loadingdialog.view.LoadingDialog
import kotlinx.android.synthetic.main.activity_charge_item.*

/**
 * 收费项目Activity
 *
 * 位置:医院综合-价格公示-收费项目
 */
class ChargeItemActivity : AppCompatActivity() {
    private var pageNum = 1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_charge_item)
        initView()
    }

    /**
     * 正在加载中弹窗显示以及收费项目条目显示
     */
    private fun initView() {
        val chargeViewModel = ViewModelProviders.of(this).get(ChargeViewModel::class.java)
        val loadingDialog = LoadingDialog(this)
        loadingDialog.setLoadingText("加载中")
            .show()
        val chargeItem: RecyclerView = findViewById(R.id.drug_price_item)
        chargeItem.layoutManager = LinearLayoutManager(this)
        val dataList: MutableList<ChargeData> = mutableListOf()
        val adapter = ChargeAdapter(this)
        val map: MutableMap<String, String> = mutableMapOf()
        map["pageSize"] = "10"
        map["pageNum"] = "$pageNum"
        chargeViewModel.getChargeByName(map) {
            dataList.addAll(it)
            adapter.notifyDataSetChanged()
        }
        adapter.dataList = dataList
        chargeItem.adapter = adapter
        loadingDialog.close()
        smart_refresh_charge.setOnLoadMoreListener {
            smart_refresh_charge.finishLoadMore(5000, true, true)
            pageNum += 1
            map.clear()
            if ("" != charge_edittext.text.toString()) {
                map["itemName"] = charge_edittext.text.toString()
            }
            map["pageSize"] = "10"
            map["pageNum"] = "$pageNum"
            chargeViewModel.getChargeByName(map) {
                dataList.addAll(it)
                adapter.notifyDataSetChanged()
            }
        }
        charge_btn.setOnClickListener {
            map.clear()
            if ("" != charge_edittext.text.toString()) {
                map["itemName"] = charge_edittext.text.toString()
            }
            map["pageSize"] = "10"
            map["pageNum"] = "1"
            chargeViewModel.getChargeByName(map) {
                if (it.size > 0) {
                    dataList.addAll(it)
                    adapter.notifyDataSetChanged()
                }
            }
        }
    }
}